<?php

namespace App\Jobs;

use App\Http\Controllers\Api\Repositories\PostsRepositores;
use App\Http\Controllers\Api\Repositories\WxCommentRepositores;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxPostsImg;
use App\Models\Posts\WxPostsVideo;
use App\Models\Posts\WxPostsFile;
use App\Models\Posts\WxPostsGoods;
use App\Models\Posts\WxPostsSound;
use App\Wen\Utils\Settings;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CalculateLikesNumJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $post_id;
    public $tries = 1;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($post_id)
    {
        //
        $this->post_id = $post_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        global $__MINI_GLOBAL_PAYCODE_9__;
        if(!$__MINI_GLOBAL_PAYCODE_9__){
            return;
        }

        if(_empty_($this->post_id)){
            return;
        }

        $robot_action_like_rate = _between_(Settings::get('robot_action_like_rate', 100), 0, 100) / 100;
        if($robot_action_like_rate == 0){
            return;
        }

        $calculate_likes_num = get_post_meta($this->post_id, 'calculate_likes_num', 'n');
        if($calculate_likes_num > 0){
            return;
        }
        $post_obj = WxPost::where('id', $this->post_id)->first();


        $img_count = 0;
        preg_replace_callback("/<(img|video).*?src[^\'\"]+[\'\"]([^\"\']+)[^>]+>/is", function ($r) use (&$img_count){
            $img_count += 1;
        }, $post_obj->posts_content);

        $img_count += WxPostsImg::where('posts_id', $post_obj->id)->count();

        $video_count = 0;
        $video_count += WxPostsVideo::where('posts_id', $post_obj->id)->count();

        $file_count = 0;
        $file_count += WxPostsFile::where('post_id', $post_obj->id)->count();

        $sounds_count = 0;
        $sounds_count += WxPostsSound::where('post_id', $post_obj->id)->count();

        $words_count = mb_strlen(strip_tags($post_obj->posts_content));

        $comments_count = WxCommentRepositores::commentCount($post_obj->id);

        $exceptional_count = PostsRepositores::exceptionalCont($post_obj->id);

        $goods_count = WxPostsGoods::where([['posts_id','=', $post_obj->id],['type', '=', 0]])->count();


        // 点赞数 = ( 视频数量 *3 + 附件数量 * 3 + 音频数量 * 3 +  图片数量 * 3 + 字数 * 0.03 + 评论 * 评论数量 * 3 + 充电数量 * 3 + 商品数量 * 3 ) * 随机系数(0.6-1)
        $like_sum = $video_count * 3 + $file_count * 3 + $sounds_count * 3 + $img_count * 3 + $words_count * 0.03 +  $comments_count * 3 +  $exceptional_count * 3 + $goods_count * 3;
        $like_sum = (int)($robot_action_like_rate * $like_sum) + mini_rand(1, 15);
        update_post_meta($post_obj->id, 'calculate_likes_num', $like_sum, 'n');
        ImitatePostlikesStepJob::dispatch($post_obj->id, $like_sum)->delay(now()->addMinutes(mini_rand(1, 5)));
    }
}
